package com.zy.teacherSide.controller;

import com.zy.teacherSide.entity.Teacher;
import com.zy.teacherSide.service.ITeacherService;
import com.zy.util.R;
import com.zy.util.RandomNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 29787
 */
@RestController
public class TeacherController {

    @Autowired
    private ITeacherService its;

    /**
     * 教师登录接口
     * @param teacher
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherLogin")
    public R teacherLogin(@RequestBody Teacher teacher) {
        Teacher t = its.teacherLogin(teacher.getTeacherNum(), teacher.getTeacherPassword());
        return new R(t == null?false:true,t);
    }

    /**
     * 教师注册接口
     * @param teacher
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherRegister")
    public R teacherRegister(@RequestBody Teacher teacher) {
        if (its.getTeacherById(teacher.getTeacherNum()) != null){
            return new R(false,"err");
        }else{
            Integer id = RandomNum.getRandomID(6);
            teacher.setTeacherId(id);
            teacher.setTeacherShiro(0);
            return new R(its.save(teacher),teacher);
        }
    }

}
